﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CustomerMaterialNoMonitor_14
{
    public partial class frmMain : Form
    {
        SqlTools sqt;

        bool bLock = false ;

        int ModifyMode = 0;

        public frmMain()
        {
            InitializeComponent();
            sqt = new SqlTools(GeneralTools.GetConnectionString("TPAMain"));
            this.Text = "14所物资代码清单维护 - " + Application.ProductVersion;
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            DoQuery();
        }

        private void DoQuery()
        {
            string strSelectString = "select a.公司型号规格,a.物资代码,a.客户型号规格 from (select distinct htb003 公司型号规格,'' 物资代码,'' 客户型号规格 from hta_table ,htb_table where hta001 = htb001 and hta014 = 'Y' and hta026 = '603车间' and hta003 like '08-14%' and hta002 between '@起始日期' and '@截止日期' and not exists(select * from DefualtDataBase.dbo.CustomerMaterialNoMapping where CustomerID = '08-14' and MyModel collate Chinese_PRC_BIN = htb003)) a order by a.公司型号规格";
            DataTable tmpdt = sqt.GetData(strSelectString.Replace("@起始日期", dtpFrom.Value.ToString("yyyyMMdd"))
                                                                                        .Replace("@截止日期", dtpTo.Value.ToString("yyyyMMdd")));
            dgv1.DataSource = tmpdt;
            if (tmpdt!=null &&
                tmpdt.Rows.Count >0)
            {
                dgv1.ReadOnly = false;
                dgv1.Columns[0].ReadOnly = true;
                dgv1.Columns[1].ReadOnly = false;
                dgv1.Columns[2].ReadOnly = false;
                chkAutoQuery.Checked = false;
                if (!this.Visible )
                {
                    this.Visible = true;
                    notifyIcon1.Visible = false;
                }
                dgv1.AutoResizeColumns();
            }
        }

        private void chkAutoQuery_CheckedChanged(object sender, EventArgs e)
        {
            dtpFrom.Enabled = !chkAutoQuery.Checked;
            dtpTo.Enabled = !chkAutoQuery.Checked;
            numericUpDown1.Enabled = !chkAutoQuery.Checked;
            btnQuery.Enabled = !chkAutoQuery.Checked;
            btnSave.Enabled = !chkAutoQuery.Checked;
            if (chkAutoQuery.Checked)
            {
                this.Visible = false;
                timer1.Interval = (int)(numericUpDown1.Value) * 1000;
            }
            else
            {
                this.Visible = true;
            }
            timer1.Enabled = chkAutoQuery.Checked;
        }

        private void frmMain_Resize(object sender, EventArgs e)
        {
            if (this.WindowState ==FormWindowState.Minimized)
            {
                this.Hide();
                notifyIcon1.Visible = true;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
           DoQuery();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (dgv1 .Rows.Count >0)
            {
                for(int i=0;i<dgv1 .Rows.Count;i++)
                {
                    //检查物资代码和客户型号规格是否填写完整
                    if (GeneralTools .GetString (dgv1 .Rows [i].Cells ["物资代码"].Value ,string.Empty )==string.Empty ||
                        GeneralTools.GetString(dgv1.Rows[i].Cells["客户型号规格"].Value, string.Empty) == string.Empty )
                    {
                        GeneralTools.ShowInfoMsg("物资代码或客户型号规格填写不完整，请填写完毕后重新操作！");
                        dgv1.CurrentCell = dgv1.Rows[i].Cells["物资代码"];
                        return;
                    }
                    //检查公司型号规格是否已存在
                    if (sqt.RecordExist("select ID from DefualtDataBase .dbo .CustomerMaterialNoMapping where CustomerID='08-14' and MyModel='"+ GeneralTools.GetString(dgv1.Rows[i].Cells["公司型号规格"].Value, string.Empty) + "'"))
                    {
                        if (MessageBox.Show ("公司型号规格" + GeneralTools.GetString(dgv1.Rows[i].Cells["公司型号规格"].Value, string.Empty)+"已存在，是否删除旧记录？点击取消将取消保存操作！","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question )==DialogResult.OK)
                        {
                            //删除现有物资代码
                            sqt.DoCmdExecuteNonQuery("delete from DefualtDataBase .dbo .CustomerMaterialNoMapping where CustomerID='08-14' and MyModel='" + GeneralTools.GetString(dgv1.Rows[i].Cells["公司型号规格"].Value, string.Empty) + "'");
                        }
                        else
                        {
                            return;
                        }
                    }
                }
                //提交物资代码和客户型号规格
                for(int i= 0; i < dgv1.Rows.Count; i++)
                {
                    sqt.DoCmdExecuteNonQuery("insert into DefualtDataBase .dbo .CustomerMaterialNoMapping (CustomerID,MaterialNo,CustomerModel,MyModel) values ('08-14','" + GeneralTools.GetString(dgv1.Rows[i].Cells["物资代码"].Value, string.Empty) + "','" + GeneralTools.GetString(dgv1.Rows[i].Cells["客户型号规格"].Value, string.Empty) + "','" + GeneralTools.GetString(dgv1.Rows[i].Cells["公司型号规格"].Value, string.Empty) + "')");
                }
                //重新查询
                DoQuery();
            }
        }

        private void btnQuery2_Click(object sender, EventArgs e)
        {
            string strSelectString = "select ID 序号,MaterialNo 物资代码,CustomerModel 客户型号规格,MyModel 公司型号规格 from DefualtDataBase .dbo .CustomerMaterialNoMapping where CustomerID='08-14'";
            if (!txtCustomerModel.Text .Trim ().Equals (string.Empty ))
            {
                strSelectString += " and CustomerModel like '" + txtCustomerModel.Text.Trim()+"'";
            }
            if (!txtMaterialNo.Text.Trim().Equals(string.Empty))
            {
                strSelectString += " and MaterialNo like '" + txtMaterialNo.Text.Trim()+"'";
            }
            if (!txtMyModel.Text.Trim().Equals(string.Empty))
            {
                strSelectString += " and MyModel like '" + txtMyModel.Text.Trim()+"'";
            }
            strSelectString += " order by ID";
            DataTable tmpdt = sqt.GetData(strSelectString);
            dgv2.DataSource = tmpdt;
            dgv2.AutoResizeColumns();
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            if (dgv2 .CurrentRow!=null)
            {
                bLock = true;
                ModifyMode = 2;
                dgv2.Enabled = false;
                txtID.Text = GeneralTools.GetString(dgv2.CurrentRow.Cells["序号"].Value, string.Empty);
                txtCustomerModel.Text = GeneralTools.GetString(dgv2.CurrentRow.Cells["客户型号规格"].Value, string.Empty);
                txtCustomerModel.ReadOnly = true;
                txtMaterialNo .Text = GeneralTools.GetString(dgv2.CurrentRow.Cells["物资代码"].Value, string.Empty);
                txtMaterialNo.ReadOnly = true;
                txtMyModel .Text = GeneralTools.GetString(dgv2.CurrentRow.Cells["公司型号规格"].Value, string.Empty);
                btnQuery2.Enabled = false;
                btnModify.Enabled = false;
                btnAdd.Enabled = false;
                btnDelete.Enabled = false;
                btnConfirm.Visible = true;
                btnCancel.Visible = true;
            }
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(bLock)
            {
                tabControl1.SelectedIndex = 1;
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            bLock = true;
            ModifyMode = 1;
            dgv2.Enabled = false;
            txtID.Clear();
            txtCustomerModel.Text = string.Empty;
            txtCustomerModel.ReadOnly = false;
            txtMaterialNo.Text = string.Empty;
            txtMaterialNo.ReadOnly = false;
            txtMyModel.Text = string.Empty;
            btnQuery2.Enabled = false;
            btnModify.Enabled = false;
            btnAdd.Enabled = false;
            btnDelete.Enabled = false;
            btnConfirm.Visible = true;
            btnCancel.Visible = true;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (dgv2.CurrentRow!=null)
            {
                string strID = GeneralTools.GetString(dgv2.CurrentRow.Cells["序号"].Value, string.Empty);
                if (MessageBox.Show ("确定删除序号为"+strID+"的项吗？","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question )==DialogResult.OK)
                {
                    sqt.DoCmdExecuteNonQuery("delete from DefualtDataBase .dbo .CustomerMaterialNoMapping where ID=" + strID);
                    btnQuery2_Click(this, new EventArgs());
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            bLock = false ;
            ModifyMode = 0;
            dgv2.Enabled = true ;
            txtID.Clear();
            txtCustomerModel.Clear();
            txtCustomerModel.ReadOnly = false;
            txtMaterialNo.Clear();
            txtMaterialNo.ReadOnly = false;
            txtMyModel.Clear();
            btnQuery2.Enabled = true;
            btnModify.Enabled = true;
            btnAdd.Enabled = true;
            btnDelete.Enabled = true;
            btnConfirm.Visible = false ;
            btnCancel.Visible = false ;
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            switch (ModifyMode)
            {
                case 1:
                    //新增模式
                    //客户型号规格
                    if (txtCustomerModel .Text .Trim ()==string.Empty )
                    {
                        GeneralTools.ShowInfoMsg("客户型号规格不能为空！");
                        txtCustomerModel.Focus();
                        return;
                    }
                    //物资代码
                    if (txtMaterialNo.Text.Trim() == string.Empty)
                    {
                        GeneralTools.ShowInfoMsg("物资代码不能为空！");
                        txtMaterialNo.Focus();
                        return;
                    }
                    if (sqt.RecordExist("select ID from DefualtDataBase .dbo .CustomerMaterialNoMapping where CustomerID='08-14' and MaterialNo='" + txtMaterialNo .Text .Trim () + "'"))
                    {
                        GeneralTools.ShowInfoMsg("物资代码" + txtMaterialNo.Text.Trim() + "已存在！");
                        txtMaterialNo.Focus();
                        return;
                    }
                    //公司型号规格
                    if (txtMyModel.Text.Trim() == string.Empty)
                    {
                        GeneralTools.ShowInfoMsg("公司型号规格不能为空！");
                        txtMyModel.Focus();
                        return;
                    }
                    if (sqt.RecordExist("select ID from DefualtDataBase .dbo .CustomerMaterialNoMapping where CustomerID='08-14' and MyModel='" + txtMyModel.Text.Trim() + "'"))
                    {
                        GeneralTools.ShowInfoMsg("公司型号规格" + txtMyModel.Text.Trim() + "已存在！");
                        txtMaterialNo.Focus();
                        return;
                    }
                    if (!sqt .RecordExist("select ycl001 from ycl_table where ycl001='"+txtMyModel.Text.Trim () +"'"))
                    {
                        GeneralTools.ShowInfoMsg("公司型号规格" + txtMyModel.Text.Trim() + "在TPAMain系统中不存在，请检查是否录入错误！");
                        txtMyModel.Focus();
                        return;
                    }
                    string strInsert= "insert into DefualtDataBase.dbo.CustomerMaterialNoMapping (CustomerID, MaterialNo, CustomerModel, MyModel) values('08-14', '" + txtMaterialNo .Text .Trim () + "', '" + txtCustomerModel .Text .Trim () + "', '" + txtMyModel .Text .Trim ()+ "')";
                    sqt.DoCmdExecuteNonQuery(strInsert);
                    GeneralTools.ShowInfoMsg("添加成功！");
                    btnClear_Click(this, new EventArgs());
                    btnQuery2_Click(this, new EventArgs());
                    break;
                case 2:
                    //修改模式
                    //公司型号规格
                    if (txtMyModel.Text.Trim() == string.Empty)
                    {
                        GeneralTools.ShowInfoMsg("公司型号规格不能为空！");
                        txtMyModel.Focus();
                        return;
                    }
                    if (sqt.RecordExist("select ID from DefualtDataBase .dbo .CustomerMaterialNoMapping where CustomerID='08-14' and MyModel='" + txtMyModel.Text.Trim() + "' and ID<>"+txtID.Text ))
                    {
                        GeneralTools.ShowInfoMsg("公司型号规格" + txtMyModel.Text.Trim() + "已存在！");
                        txtMaterialNo.Focus();
                        return;
                    }
                    if (!sqt.RecordExist("select ycl001 from ycl_table where ycl001='" + txtMyModel + "'"))
                    {
                        GeneralTools.ShowInfoMsg("公司型号规格" + txtMyModel.Text.Trim() + "在TPAMain系统中不存在，请检查是否录入错误！");
                        txtMyModel.Focus();
                        return;
                    }
                    string strUpdate = "update DefualtDataBase.dbo.CustomerMaterialNoMapping set MyModel='" + txtMyModel.Text.Trim() + "' where ID="+txtID.Text ;
                    sqt.DoCmdExecuteNonQuery(strUpdate);
                    GeneralTools.ShowInfoMsg("修改成功！");
                    btnClear_Click(this, new EventArgs());
                    btnQuery2_Click(this, new EventArgs());
                    break;
            }
            bLock = false;
            ModifyMode = 0;
            dgv2.Enabled = true;
            txtID.Clear();
            txtCustomerModel.Clear();
            txtCustomerModel.ReadOnly = false;
            txtMaterialNo.Clear();
            txtMaterialNo.ReadOnly = false;
            txtMyModel.Clear();
            btnQuery2.Enabled = true;
            btnModify.Enabled = true;
            btnAdd.Enabled = true;
            btnDelete.Enabled = true;
            btnConfirm.Visible = false;
            btnCancel.Visible = false;
            btnQuery2_Click(this, new EventArgs());
        }

        private void dgv2_CurrentCellChanged(object sender, EventArgs e)
        {
            if (dgv2.CurrentRow!=null)
            {
                txtID.Text = GeneralTools.GetString(dgv2.CurrentRow.Cells["序号"].Value, string.Empty);
                txtMaterialNo.Text = GeneralTools.GetString(dgv2.CurrentRow.Cells["物资代码"].Value, string.Empty);
                txtCustomerModel.Text = GeneralTools.GetString(dgv2.CurrentRow.Cells["客户型号规格"].Value, string.Empty);
                txtMyModel.Text = GeneralTools.GetString(dgv2.CurrentRow.Cells["公司型号规格"].Value, string.Empty);
            }
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            //this.WindowState = FormWindowState.Normal;
            this.Show();
            this.Focus();
            notifyIcon1.Visible = false;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtID.Clear();
            txtCustomerModel.Clear();
            txtMaterialNo.Clear();
            txtMyModel.Clear();
        }

        private void tsmiOnlySaveSelected_Click(object sender, EventArgs e)
        {
            if (dgv1.SelectedRows.Count > 0)
            {
                for (int i = 0; i < dgv1.SelectedRows.Count; i++)
                {
                    //检查物资代码和客户型号规格是否填写完整
                    if (GeneralTools.GetString(dgv1.SelectedRows[i].Cells["物资代码"].Value, string.Empty) == string.Empty ||
                        GeneralTools.GetString(dgv1.SelectedRows[i].Cells["客户型号规格"].Value, string.Empty) == string.Empty)
                    {
                        GeneralTools.ShowInfoMsg("物资代码或客户型号规格填写不完整，请填写完毕后重新操作！");
                        dgv1.CurrentCell = dgv1.SelectedRows[i].Cells["物资代码"];
                        return;
                    }
                    //检查公司型号规格是否已存在
                    if (sqt.RecordExist("select ID from DefualtDataBase .dbo .CustomerMaterialNoMapping where CustomerID='08-14' and MyModel='" + GeneralTools.GetString(dgv1.SelectedRows[i].Cells["公司型号规格"].Value, string.Empty) + "'"))
                    {
                        if (MessageBox.Show("公司型号规格" + GeneralTools.GetString(dgv1.SelectedRows[i].Cells["公司型号规格"].Value, string.Empty) + "已存在，是否删除旧记录？点击取消将取消保存操作！", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                        {
                            //删除现有物资代码
                            sqt.DoCmdExecuteNonQuery("delete from DefualtDataBase .dbo .CustomerMaterialNoMapping where CustomerID='08-14' and MyModel='" + GeneralTools.GetString(dgv1.SelectedRows[i].Cells["公司型号规格"].Value, string.Empty) + "'");
                        }
                        else
                        {
                            return;
                        }
                    }
                }
                //提交物资代码和客户型号规格
                for (int i = 0; i < dgv1.SelectedRows.Count; i++)
                {
                    sqt.DoCmdExecuteNonQuery("insert into DefualtDataBase .dbo .CustomerMaterialNoMapping (CustomerID,MaterialNo,CustomerModel,MyModel) values ('08-14','" + GeneralTools.GetString(dgv1.SelectedRows[i].Cells["物资代码"].Value, string.Empty) + "','" + GeneralTools.GetString(dgv1.SelectedRows[i].Cells["客户型号规格"].Value, string.Empty) + "','" + GeneralTools.GetString(dgv1.SelectedRows[i].Cells["公司型号规格"].Value, string.Empty) + "')");
                }
                //重新查询
                DoQuery();
            }
        }
    }
}
